package com.tbit.uqbike.client.service.impl;

import com.tbit.common.constant.RideLogDetailsConstant;
import com.tbit.uqbike.client.dao.capital.RideLogDetailsDao;
import com.tbit.uqbike.client.dao.core.UserDao;
import com.tbit.uqbike.client.pojo.Result;
import com.tbit.uqbike.client.pojo.RideLogDetails;
import com.tbit.uqbike.client.pojo.User;
import com.tbit.uqbike.client.service.RideLogDetailsService;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * 骑行订单详情
 *
 * @Author:BUGTian
 * @DATE: 2023/5/22
 */
@Service
public class RideLogDetailsServiceImpl implements RideLogDetailsService {
    @Autowired
    private RideLogDetailsDao rideLogDetailsDao;
    @Autowired
    private UserDao userDao;

    @Override
    public Result<RideLogDetails> queryByOrderId(String orderNO) {
        RideLogDetails rideLogDetails = rideLogDetailsDao.getByOrderId(orderNO);
        if (rideLogDetails != null) {
            //未支付订单才查用户余额
            if (RideLogDetailsConstant.ORDER_ARREARS.equals(rideLogDetails.getPayStatus())) {
                User user = userDao.getByUserId(rideLogDetails.getUserId());
                rideLogDetails.computeWaitingPayMoney(user.getMoney().longValue());
            } else {
                rideLogDetails.setWaitingPayMoney(0L);
            }
            //430版本二期需求，要把订单详情字段里的停车时长、停车费用去掉
            this.removeStopParam(rideLogDetails);
        }

        return Result.success(rideLogDetails);
    }

    @Override
    public Result<List<RideLogDetails>> list(Integer userId) {
        List<RideLogDetails> rideLogDetailsList = rideLogDetailsDao.selectByUserId(userId);
        //430版本二期需求，要把订单详情字段里的停车时长、停车费用去掉
        this.removeStopTime(rideLogDetailsList);

        return Result.success(rideLogDetailsList);
    }

    /**
     * @description: 获取最近的指定条数的订单
     * @author: mo.shanyong
     * @date: 2025/5/13 15:10
     * @param: machineId 车辆id
     * @param: limit 限制条数
     * @return: java.util.List<com.tbit.uqbike.client.pojo.RideLogDetails>
     */
    @Override
    public List<RideLogDetails> getLastLimitByMachineId(Integer machineId, Integer limit) {
        return rideLogDetailsDao.getLastLimitByMachineId(machineId,limit);
    }

    /**
     * @description: 批量去除掉订单详情里面的停车时长、停车费用
     * @author: mo.shanyong
     * @date: 2024/12/10 17:12
     * @param: rideLogDetailsList
     */
    private void removeStopTime(List<RideLogDetails> rideLogDetailsList){
        if (CollectionUtils.isEmpty(rideLogDetailsList))
            return;
        for (RideLogDetails rideLogDetails : rideLogDetailsList) {
            this.removeStopParam(rideLogDetails);
        }
    }

    /**
     * @description: 单个去除掉订单详情里面的停车时长、停车费用
     * @author: mo.shanyong
     * @date: 2024/12/10 17:12
     * @param: rideLogDetailsList
     */
    private void removeStopParam(RideLogDetails rideLogDetails){
        if (StringUtils.isBlank(rideLogDetails.getBillingDetails()))
            return;
        String[] split = rideLogDetails.getBillingDetails().split("[,，]");
        List<String> list = new ArrayList<>();
        //标志位，看看是否存在停车时长
        boolean haveStopTime = false;
        for (String s : split) {
            //去除掉停车时长
            if (s.contains("停车时长") || s.contains("停车费用")){
                haveStopTime = true;
                continue;
            }
            list.add(s);
        }
        //没有停车时长，不需要替换订单详情，直接跳下一个
        if (!haveStopTime){
            return;
        }
        rideLogDetails.setBillingDetails(String.join(",",list));
    }
}
